package com.example.app18_service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app18_service.service.HelloService;
import com.example.app18_service.service.MyService;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private ServiceConnection connection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");


    }

    /**
     * 启动服务
     * @param view 发生点击事件的按钮对象
     */
    public void startMyService(View view) {
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        Toast.makeText(this, "Start Service...", Toast.LENGTH_SHORT).show();
    }

    /**
     * 停止服务
     * @param view 发生点击事件的按钮对象
     */
    public void stopMyService(View view) {
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);
        Toast.makeText(this, "Stop Service...", Toast.LENGTH_SHORT).show();
    }

    /**
     * 绑定服务
     * @param view 发生点击事件的按钮对象
     */
    public void bindMyService(View view) {
        Intent intent = new Intent(this, MyService.class);
        if (connection == null) {
            connection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "ServiceConnection onServiceConnected()");
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "ServiceConnection onServiceDisconnected()");
                }
            };
            // 创建绑定服务
            // 想与应用中的Activity和其他组件提供交互，或通过进程间通信（IPC）向其他应用公开应用的某些功能时
            // 使用创建绑定服务
            // 需要实现onBind() 方法返回IBinder，用于定义与服务通信的接口
            bindService(intent, connection, Context.BIND_AUTO_CREATE);
            Toast.makeText(this, "Bind Service...", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "已绑定Service...", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 解绑服务
     * @param view 发生点击事件的按钮对象
     */
    public void unbindMyService(View view) {
        if (connection != null) {
            // 解除绑定服务
            unbindService(connection);
            connection = null;
            Toast.makeText(this, "Unbind Service...", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "还没有绑定Service...", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Activity 销毁是调用解绑方法
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
            connection = null;
        }
    }

    /**
     * 启动HelloService服务
     * @param view view
     */
    public void startHelloService(View view) {
        // startService() 方法会立即返回
        // 并且Android系统会调用服务的onStartCommand() 方法
        // 如果服务尚未运行，系统会先调用onCreate()，再调用onStartCommand()
        // 多个服务的调用会导致 onStartCommand() 方法调用多次
        // 但是只需要一个服务停止请求即可停止服务
        // stopSelf() 或者 stopService() 都可以停止服务
        startService(new Intent(this, HelloService.class));
    }

    /**
     * 停止HelloService服务
     * @param view view
     */
    public void stopHelloService(View view) {
        // 停止服务
        stopService(new Intent(this, HelloService.class));
    }
}